﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using VmGui.localhost;
using System.Web.Services.Protocols; //utilizado para capturar as validações no servidor



namespace VmGui
{
    public partial class FormCrudCliente : Form
    {

        List<Cliente> listaCliente;

        List<Cliente> pesquisaCliente;

        public FormCrudCliente()
        {
            InitializeComponent();
        }

        private void bNovoCliente_Click(object sender, EventArgs e)
        {

            FormNovoCliente frmNovoCLi = new FormNovoCliente();

            frmNovoCLi.ShowDialog();

            listar_Cliente();
        }


        public void listar_Cliente()
        {

            this.Cursor = Cursors.WaitCursor;


            Service1 sv = new Service1();


            try
            {
                listaCliente = sv.listar_Cli().ToList();
                dataGridView1.DataSource = listaCliente;
            }
            catch (SoapException erro)
            {

                MessageBox.Show(erro.Message);
            }

            this.Cursor = Cursors.Default;


        }

        private void FormCrudCliente_Load(object sender, EventArgs e)
        {

            listar_Cliente();

        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {



            //MessageBox.Show(listaCliente.ElementAt(dataGridView1.SelectedRows[0].Index).CodCli.ToString()+"\n"+listaCliente.ElementAt(dataGridView1.SelectedRows[0].Index).RazaoSocial.ToString());

        }

        private void bExcluirCliente_Click(object sender, EventArgs e)
        {

            Service1 sv = new Service1();

            Cliente cli = new Cliente();


            try
            {
                int x =Int32.Parse( dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

                cli.CodCli = x;

                if (MessageBox.Show("Tem certeza que deseja excluir ?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (sv.verificar(cli) == false)
                    {
                        sv.excluir_Cli(cli);

                        MessageBox.Show("Registro Excluído com sucesso!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);


                        listar_Cliente();
                    }


                    else{
                        MessageBox.Show("Cliente não pode ser excluído porque existem pedidos vinculados a ele","Atenção",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    
                    }
                }


            }
            catch (SoapException erro)
            {

                MessageBox.Show(erro.Message);
            }

        }

        private void bAlterarCliente_Click(object sender, EventArgs e)
        {

            FormAlterarCliente fac = new FormAlterarCliente();

            try
            {
                fac.tbCodigo.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                fac.tbRazaoAlter.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                fac.maskedtbCnpjAlter.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                fac.tbEnderecoAlter.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                fac.tbCidadeAlter.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                fac.comboUfAlter.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                fac.tbContatoAlter.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
                fac.maskedTelefoneAlter.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
                fac.ShowDialog();
                listar_Cliente();
            }
            catch (Exception)
            {
                MessageBox.Show("Registro não selecionado para alteração", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            textBox2.Clear();
            Service1 sv = new Service1();
            pesquisaCliente = sv.pesquisar_CliRazao(textBox1.Text).ToList();

            dataGridView1.DataSource = pesquisaCliente;

        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            textBox1.Clear();
            Service1 sv = new Service1();
            pesquisaCliente = sv.pesquisar_CliCnpj(textBox2.Text).ToList();

            dataGridView1.DataSource = pesquisaCliente;
        }
    }
}
